﻿// owner 蒋家伟

#include "ApplicationEventHandler.h"

#include "IApplicationEventArgs.h"
#include "IUnitTestManager.h"
#include "IApplicationEvent.h"
#include "IApplication.h"

using namespace jsdk;

namespace
{
    ApplicationHandler s_ApplicationHandler;

    bool hasStartUnitTest(const std::vector<std::string>& res)
    {
        return std::find_if(res.begin(), res.end(), [](const std::string& str) {
            return str == "StartUnitTest";
        }) != res.end();
    }
}

jsdk::ApplicationHandler::ApplicationHandler()
{
    IApplicationEvent::get()->registerHandler(this);
}

jsdk::ApplicationHandler::~ApplicationHandler()
{
    IApplicationEvent::get()->unRegisterHandler(this);
}

void jsdk::ApplicationHandler::On(const IArgs* pArgs)
{
    if (const IApplicationEventArgs* pApplicationEventArgs = dynamic_cast<const IApplicationEventArgs*>(pArgs))
    {
        if (pApplicationEventArgs->getApplicationStatus() == ApplicationStatus::AfterStart)
        {
            if (hasStartUnitTest(IApplication::get()->getCmdParameter()))
            {
                if (IUnitTestManager::get().start())
                {
                    // 单元测试结束关闭程序
                    IApplication::get()->exit();
                    system("pause");
                }
            }
            return;
        }
    }
}
